package net.htjs.mobile.nyoa;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import net.htjs.mobile.nyoa.util.L;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity {
	private Button login;
	EditText userN;
	EditText pssW;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		userN = (EditText) this.findViewById(R.id.username_edit);
		userN.setText("songyunxiang");
		pssW = (EditText) this.findViewById(R.id.password_edit);
		pssW.setText("111111");
		login = (Button) this.findViewById(R.id.signin_button);
		login.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				HttpPost request = new HttpPost(L.loginUrl + "userId="
						+ userN.getText().toString() + "&password="
						+ pssW.getText().toString());
				// 先封装一个 JSON 对象 /server/3g/login4Mobile.jsp
				JSONObject param = new JSONObject();
				try {
					param.put("userId", userN.getText().toString());
					param.put("password", pssW.getText().toString());
					// 绑定到请求 Entry
					StringEntity se = new StringEntity(param.toString());
					request.setEntity(se);
					// 发送请求
					DefaultHttpClient client=new DefaultHttpClient();
					HttpResponse httpResponse = client
							.execute(request);
					// 得到应答的字符串，这也是一个 JSON 格式保存的数据
					String retSrc = EntityUtils.toString(httpResponse
							.getEntity());

					List<Cookie> cookies = client.getCookieStore().getCookies();  
					if (!cookies.isEmpty()) {  
					    for (int i = 0; i < cookies.size(); i++) {  
					        L.cookie = cookies.get(i);  
					    }  
					}
					// 生成 JSON 对象
					System.out.println(retSrc);
					JSONObject i =  new JSONObject(retSrc.trim());
					if ("1".equals(i.get("result"))) {
						L.LoginInfo.userName=userN.getText().toString();
						L.LoginInfo.password=pssW.getText().toString();
						//System.out.println(i.get("JSESSIONID"));
						Intent mainIntent = new Intent(getApplication(),
								TabMainActivity.class);
						startActivity(mainIntent);
					} else if ("4".equals(i)) {
						AlertDialog.Builder builder = new Builder(
								LoginActivity.this);
						builder.setTitle("提示");
						builder.setMessage("非法访问");
						Dialog dialog = builder.create();
						builder.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										// 这里添加点击确定后的逻辑
										dialog.dismiss();
									}
								});

						builder.create().show();

					} else if ("5".equals(i)) {
						// response.sendRedirect("main.jsp");
					} else if ("0".equals(i)) {
						AlertDialog.Builder builder = new Builder(
								LoginActivity.this);

						builder.setTitle("提示");
						builder.setMessage("账户被禁用");
						Dialog dialog = builder.create();
						builder.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										// 这里添加点击确定后的逻辑
										dialog.dismiss();
									}
								});

						builder.create().show();
					} else if ("3".equals(i)) {
						AlertDialog.Builder builder = new Builder(
								LoginActivity.this);
						builder.setTitle("提示");
						builder.setMessage("用户名无效");
						Dialog dialog = builder.create();
						builder.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										// 这里添加点击确定后的逻辑
										dialog.dismiss();
									}
								});

						builder.create().show();
					} else {
						AlertDialog.Builder builder = new Builder(
								LoginActivity.this);
						builder.setTitle("提示");
						builder.setMessage("密码错误");
						Dialog dialog = builder.create();
						builder.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										// 这里添加点击确定后的逻辑
										dialog.dismiss();
									}
								});

						builder.create().show();
					}
					// JSONObject result = new JSONObject(retSrc);
					// String token = (String) result.get("token");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		});
	}
}